package com.yueliang.controller;

import com.alibaba.fastjson.JSONObject;
import com.yueliang.bean.Key;
import com.yueliang.service.DeviceService;
import com.yueliang.service.impl.DeviceServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.spring.web.json.Json;

import java.io.IOException;

@CrossOrigin
@RestController
@RequestMapping("/device")
public class DeviceController {

    @Autowired
    private DeviceService deviceService;


    /**
     * 获取设备列表信息
     *
     * @return
     * @throws IOException
     */
    @GetMapping("/list")
    public JSONObject getDeviceList() throws IOException {
        String deviceList = deviceService.getDeviceList();
        return JSONObject.parseObject(deviceList);
    }

    /**
     * 获取指定设备的列表详情
     *
     * @param deviceId
     * @return
     */
    @GetMapping("/details/{deviceId}")
    public JSONObject getDeviceDetailsById(@PathVariable("deviceId") String deviceId) {
        return deviceService.getDeviceDetailsById(deviceId);
    }


    /**
     * 更新某个设备key
     *
     * @param deviceId      设备号
     * @param key
     * @param force_offline
     * @return
     * @throws IOException
     */
    @GetMapping("/{id}/{key}/{force_offline}")
    public Boolean updateDeviceKey(@PathVariable("id") String deviceId, @PathVariable Key key, @PathVariable("force_offline") Boolean force_offline) throws IOException {
        System.out.println("某个设备的key" + deviceId);
        System.out.println("--------" + key.getKey());
        System.out.println(force_offline);
        Boolean isUpdate = deviceService.updateDeviceKey(deviceId, key, force_offline);
        return isUpdate;
    }

    /**
     * 获取随机base64编码格式key
     *
     * @return
     */
    @GetMapping("/getBase64Key")
    public JSONObject baseKey() {
        return deviceService.getBase64key();
    }

    /**
     * 查询某个设备镜像
     *
     * @param deviceId
     * @return
     * @throws IOException
     */
    @GetMapping("/{deviceId}/image")
    public JSONObject getDeviceImage(@PathVariable String deviceId) throws IOException {
        return deviceService.getDeviceImage(deviceId);
    }


    /**
     * 根据某个设备Id删除设备
     *
     * @param deviceId
     * @return
     * @throws IOException
     */
    @GetMapping("/{deviceId}")
    public Boolean deleteDevice(@PathVariable String deviceId) throws IOException {
        return deviceService.deleteDeviceById(deviceId);
    }

    @GetMapping("/data/{deviceId}")
    public JSONObject getDeviceData(@PathVariable String deviceId) throws IOException {
        return deviceService.getDeviceData(deviceId);
    }


    /**
     * 点击下发命令下发指令
     *
     * @param deviceId
     * @param content
     * @param timeout
     * @return
     * @throws IOException
     */
    @CrossOrigin(origins = "http://localhost:9528", allowCredentials = "true")
    @RequestMapping("/sendDirective/{deviceId}/{content}/{timeout}")
    public String sendDirective(@PathVariable String deviceId, @PathVariable String content, @PathVariable Integer timeout) throws IOException {
        return deviceService.sendDirective(deviceId, content, timeout);
    }
}
